package com.csnf.servlet;

import com.csnf.entity.User;
import com.csnf.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user")
public class UserServlet2 extends HttpServlet {
    UserService userService = new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从登录页面上获取用户名，密码和权限id
        String uName=req.getParameter("uName");
        String passWord=req.getParameter("passWord");
        String roleId=req.getParameter("roleId");
        //后台打印结果
        System.out.println(uName+":"+passWord+":"+roleId);
        List<User> ulist=userService.getUser(uName);
        System.out.println(ulist);
        //数据库有该用户
        if(ulist.size()>0){
            //密码和权限都正确
            if(ulist.get(0).getPassWord().equals(passWord) && ulist.get(0).getRoleId().equals(roleId) ){
                req.getRequestDispatcher("singger?type=all").forward(req, resp);
            }else{
                req.setAttribute("msg","1");
                //登录失败不能直接返回login.jsp，要通过访问/role 调用权限查询再返回login.jsp
                req.getRequestDispatcher("role").forward(req, resp);
            }
        }else{
            req.setAttribute("msg","0");
            //没有改用户
            req.getRequestDispatcher("role").forward(req, resp);
        }
    }
}
